package com.leetcode;

public class P0404SumOfLeftLeaves {

	
	public static void main(String[] args) {
		TreeNode node1 = new TreeNode(1);
		TreeNode node2 = new TreeNode(2);
		TreeNode node3 = new TreeNode(3);
		TreeNode node4 = new TreeNode(4);
		TreeNode node5 = new TreeNode(5);

		node1.left = node2;
		node1.right = node3;
		node2.left = node4;
		node2.right = node5;


		System.out.println("count = " + sumOfLeftLeaves(null));
		System.out.println("count = " + sumOfLeftLeaves(node1));
	}

	public static int sumOfLeftLeaves(TreeNode root) {
		int count = 0;
		if(root != null){
			if(root.left != null){
				if(root.left.left == null && root.left.right == null){
					count += root.left.val;
				}
				count += sumOfLeftLeaves(root.left);
			}
			if(root.right != null){
				count += sumOfLeftLeaves(root.right);
			}
		}

		return count;
	}


	
}


class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode(int x) {
		val = x;
	}
}

